<?php

class DocumentsController extends FrontController
{
	public function actionIndex()
	{
		//$this->render('index');
	}
    
    public function actionView()
    {
        $doc_id = $this->get('id',0);
        $doc_alias = $this->get('alias','');
        $locale = $this->get('locale','');
        $cat_id = $this->get('cat_id',0);
        $model = LanguageDocument::model()->with('document')->find(
            'document_id=:docId AND alias=:alias AND locale=:locale AND document.category_id=:catId AND document.status=:status',
            array(
                ':docId' => $doc_id,
                ':alias' => $doc_alias,
                ':locale' => $locale,
                ':catId' => $cat_id,
                ':status' => Document::STATUS_ACTIVE,
            )
        );
        if (is_null($model))
            throw new CHttpException(404,Yii::t('site/general','DOCUMENT_NOT_FOUND'));
        $model->document->views++;
        $model->document->update(array('views'));
        $this->pageTitle = $this->getSiteNameByLanguage().' | '.$model->title;
        $this->render('view',array('model'=>$model));
    }
    
    public function actionListByCategory()
    {
        $cat_id = $this->get('id',0);
        $alias = $this->get('alias','');
        $locale = $this->get('locale','');
        $cat = LanguageCategory::model()->with('category')->find(
            't.category_id=:catId AND t.alias=:alias AND locale=:locale AND category.is_active = 1',
            array(
                ':catId' => $cat_id,
                ':alias' => $alias,
                ':locale' => $locale,
            )
        );
        if (is_null($cat))
            throw new CHttpException(404,Yii::t('site/general','CATEGORY_NOT_FOUND'));
        
        $this->pageTitle = $this->getSiteNameByLanguage().' | '.$cat->title;
        $this->render('list_by_category',array('cat'=>$cat));    
    }
    
    public function actionDownload()
    {
        $doc_id = $this->get('id',0);
        $doc = Document::model()->find('id=:id AND status=:status',array(':id'=>$doc_id,':status'=>Document::STATUS_ACTIVE));
        if (is_null($doc))
            throw new CHttpException(404, 'This document is not found.');
        if (empty($doc->file_upload))
            throw new CHttpException(404, 'This document has no file.');
        $file_path = $_SERVER['DOCUMENT_ROOT'].$doc->file_upload;
        if (!file_exists($file_path))
            throw new CHttpException(404, 'Document File does not exist.');
        $arrFilePathParts = explode('/',$file_path);
        $file_name = trim(str_replace(' ','',end($arrFilePathParts)));
        // download
        header("Content-Type: application/force-download");
        header("Content-Length: ".filesize($file_path));
        header("Content-Disposition: attachment; filename=".$file_name);
        header("Content-Transfer-Encoding: binary");
        $fp = fopen($file_path, "rb");
        if ($fp)
        {
            $doc->downloads++;
            $doc->update(array('downloads'));
            fpassthru($fp);
            fclose($fp);
        }
        else
            throw new CHttpException(404, 'File cannot be downloaded.');       
    }
}